package graphics;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import willispace.Directions;
import willispace.WilliSpace;

/**
 * Control Window Class
 * Specifies look and actions in control window
 * @author mateusz
 */
public class ControlWindow extends CustomWindow implements MouseListener {

    /**
     * main panel of this window
     */
    public JPanel flowPanel;
    /**
     * item of flow panel, containing available directions
     */
    public JPanel directionsPanel;
    /**
     * item of flow panel, containing controll buttons
     */
    public JPanel controlPanel;
    private Btn pauseButton, exitButton;
    private HashMap<String, Btn> directionsButtons;

    /**
     * Creates control window
     * @param name      Name of control window
     * @param size      Size of control window
     * @param position  Position of control window
     */
    public ControlWindow(String name, Dimension size, Point position) {
        this(name, size.width, size.height, position.x, position.y);
    }

    /**
     * Creates control window
     * @param name      Name of control window
     * @param width     Width of control window
     * @param height    Height  of control window
     * @param posX      Position X (horizontal) of control window
     * @param posY      Position Y (vertical) of control window
     */
    public ControlWindow(String name, int width, int height, int posX,
            int posY) {
        // call parent constructor
        super(name, width, height, posX, posY);
        addMainPanel();
        setAlwaysOnTop(true);                             // keep this window always on top

        // call proper properties file
        rb = java.util.ResourceBundle.getBundle("graphics/ControlWindow");

        directionsButtons = new HashMap<String, Btn>();


        setLayout(new FlowLayout(FlowLayout.CENTER));
        // add panel
        flowPanel = new JPanel();
        flowPanel.setLayout(new BoxLayout(flowPanel, BoxLayout.X_AXIS));



        // directions panel with directions buttons
        directionsPanel = new JPanel();
        directionsPanel.setLayout(new GridLayout(3, 3));

        String[] txts = {"", rb.getString("UP"), "", rb.getString("LEFT"), "", rb.getString("RIGHT"), "", rb.getString("DOWN"), ""};
        for (int i = 0; i < txts.length; i++) {
            int x = 0, y = 0;

            if (i == 3) {
                x = -1;
            } else if (i == 5) {
                x = 1;
            }

            if (i == 1) {
                y = -1;
            } else if (i == 7) {
                y = 1;
            }
            Btn btn = new Btn(txts[i], this, x, y);
            if (x == 0 && y == 0) {
                btn.setEnabled(false);
            } else {
                directionsButtons.put(txts[i].toLowerCase(), btn);
            }
            directionsPanel.add(btn);
        }


        // control Panel
        controlPanel = new JPanel();
        controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
        controlPanel.add(pauseButton = new Btn(rb.getString("PAUSE_PLAY"), this));
        controlPanel.add(exitButton = new Btn(rb.getString("END"), this));
        pauseButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        exitButton.setAlignmentX(Component.CENTER_ALIGNMENT);

        flowPanel.add(directionsPanel);
        flowPanel.add(controlPanel);

        mainPanel.add(flowPanel);
        validate();
    }

    /**
     * Move map in specified by x and y direction
     * Updates directions buttons
     * @param x Horizontal move of map
     * @param y Vertical move of map
     */
    public void updateDirections(int x, int y) {
        Directions<Boolean> d = WilliSpace.moveMap(x, y);
        directionsButtons.get("left").setEnabled(d.left);
        directionsButtons.get("right").setEnabled(d.right);
        directionsButtons.get("up").setEnabled(d.up);
        directionsButtons.get("down").setEnabled(d.down);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Btn btn = (Btn) e.getSource();
        // one of directions buttons
        if ((btn.xMove != 0 || btn.yMove != 0) && btn.isEnabled()) {
            updateDirections(btn.xMove, btn.yMove);
        } // play/pause button
        else if (btn == pauseButton) {
            WilliSpace.pause();
        } 
        // exit button
        else if (btn == exitButton) {
            WilliSpace.stop();
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}

class Btn extends JButton {

    int xMove, yMove;

    public Btn(String text, ControlWindow c) {
        setText(text);
        addMouseListener(c);
    }

    public Btn(String text, ControlWindow c, int xMove, int yMove) {
        this(text, c);
        this.xMove = xMove;
        this.yMove = yMove;
    }
}